package cn.itcast.crm.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.itcast.crm.eum.ErrCode;
import cn.itcast.crm.eum.SuccCode;
import cn.itcast.crm.po.User;
import cn.itcast.crm.utils.CacheUtils;

@Controller
@RequestMapping("/user")
public class UserController {
	private static final Logger log = Logger.getLogger(UserController.class);
	private static final String LOGIN_USER="LOGIN_USER";
//    @Resource
//    private SysUserService sysUserService;
    @RequestMapping("/login")
    public ModelAndView login(Model model,HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    	//获取缓存类
    	String userName = request.getParameter("userName");
    	String password = request.getParameter("password");
    	//参数不为空判断
    	if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
    		model.addAttribute("code",ErrCode.ParaVal_Null_Err.getValue());
    		model.addAttribute("info",ErrCode.ParaVal_Null_Err.getValue());
    		log.debug(ErrCode.ParaVal_Null_Err.getDesc());
    		return new ModelAndView("login");
    	}
    	CacheUtils cacheUtils = CacheUtils.getCacheUtils();
    	List<User> userList = cacheUtils.get("userList");
    	for (User u : userList) {
    		if(userName.equals(u.getUserName())) {
    			if(password.equals(u.getPassword())) {
    				model.addAttribute("code",SuccCode.SUCCCODE.getValue());
    	    		model.addAttribute("info",SuccCode.SUCCCODE.getValue());
    	    		model.addAttribute("userName",u.getUserName());
    				request.getSession().setAttribute(userName, true);
    				request.getSession().setAttribute(LOGIN_USER, userName);
    				request.getSession().setMaxInactiveInterval(1800);
    				log.debug(SuccCode.SUCCCODE.getDesc());
    				return new ModelAndView("index");
    			}else {
    				log.debug(ErrCode.ParaPassword_Err.getDesc());
    				model.addAttribute("code",ErrCode.ParaPassword_Err.getValue());
    				model.addAttribute("info",ErrCode.ParaPassword_Err.getDesc());
    				return new ModelAndView("login");
    			}
    		}
		}
    	log.debug(ErrCode.ParaUserName_Err.getDesc());
    	model.addAttribute("code",ErrCode.ParaUserName_Err.getValue());
		model.addAttribute("info",ErrCode.ParaUserName_Err.getDesc());
    	return new ModelAndView("login");
    }
    
    @RequestMapping(value="/goodsType", method = {RequestMethod.GET, RequestMethod.POST})
    public void goodsType(Model model,HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    	model.addAttribute("code",SuccCode.SUCCCODE.getValue());
    	response.sendRedirect("view/goodsTpye.jsp");
    }
}